package com.quik.web.worker.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.quik.common.Constants;
import com.quik.dto.PaginationDto;
import com.quik.entity.SysAccount;
import com.quik.entity.DtaWorkerType;
import com.quik.exception.ErrorCode;
import com.quik.web.worker.service.WorkerService;

/**
 * 功能说明：
 *        技师类型管理的业务控制
 * @author Jenny
 *
 */
@Controller
@RequestMapping("/workerType")
public class WorkerTypeController {
	private static Logger log = (Logger)Logger.getLogger(WorkerTypeController.class);
	
	@Autowired
	private WorkerService workerService;
	
	/**
	 * 进入技师类型管理界面
	 * @return
	 */
	@RequestMapping(value = "workerTypeList", method = RequestMethod.GET)
	public String workerTypeList(){
		return "/worker/workerType/workerTypeList";
	}
	
	/**
	 * 根据条件查询技师类型
	 * @param page
	 * @param rows
	 * @param workerTypeName
	 * @param printerNo
	 * @return
	 */
	@RequestMapping(value = "queryWorkerType", method = RequestMethod.POST)
	@ResponseBody
	public PaginationDto queryWorkerType(Integer page, Integer rows, String workerTypeName, Integer printerNo){
		PaginationDto dto = new PaginationDto(0, null);
		
		try{
			int intPage = (page == null || page == 0) ? 1:page;  //当前页
		    int pageSize = (rows == null || rows == 0) ? Constants.TREE_PAGE_SIZE : rows;  //每页显示的记录
		    
		    int totalCount = workerService.findWorkerTypeCount(workerTypeName, printerNo);
		    List list = workerService.findWorkerType(workerTypeName, printerNo, intPage, pageSize);
		    
		    dto.setTotal(totalCount);
		    dto.setRows(list);
		}catch(Exception e){
			e.printStackTrace();
			log.error("WorkerTypeController queryWorkerType error: " + e.getMessage());
		}
		
		return dto;
	}
	
	/**
	 * 进入新增技师类型界面
	 * @return
	 */
	@RequestMapping(value = "initAddWorkerType", method = RequestMethod.GET)
	public String initAddWorkerType(){
		return "/worker/workerType/addWorkerType";
	}
	
	/**
	 * 检查技师类型名称是否重复
	 * @param workerType
	 * @return
	 */
	public ResponseEntity<String> checkWorkerTypeName(String workerType){
		boolean flag = false;
		
		try{
			flag = workerService.checkWorkerTypeName(workerType);
		}catch(Exception e){
			e.printStackTrace();
			log.error("WorkerTypeController checkWorkerTypeName error: " + e.getMessage());
		}
		
		return new ResponseEntity<String>(String.valueOf(flag), HttpStatus.OK);
	}
	
	/**
	 * 新增技师类型
	 * @param workerType
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addWorkerType", method = RequestMethod.POST)
	public ModelAndView addWorkerType(DtaWorkerType workerType, HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/worker/workerType/addWorkerType");
		String result = ErrorCode.RESULT_ADD_WORKER_TYPE_FAILED;
		SysAccount account = (SysAccount)request.getSession().getAttribute(Constants.SESSION_ACCOUNT);
		
		try{
			workerType.setCrt_user(account.getStaff_no());
			workerType.setCrt_date(new java.util.Date(System.currentTimeMillis()));
			
			workerService.addWorkerType(workerType);
			result = ErrorCode.RESULT_ADD_WORKER_TYPE_SUCCESS;
			
		}catch(Exception e){
			result = ErrorCode.SYSTEM_ERROR;
			e.printStackTrace();
			log.error("WorkerTypeController addWorkerType error: " + e.getMessage());
		}
		mav.addObject("result", result);
		
		return mav;
	}
}
